package com.jintoufs.web.action;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.jhj.common.util.JhjResponseUtil;
import com.jintoufs.domain.customer.Customer;
import com.jintoufs.domain.customer.CustomerResource;
import com.jintoufs.domain.security.Resource;
import com.jintoufs.domain.user.User;
import com.jintoufs.service.customer.CustomerResourceService;
import com.jintoufs.service.security.ResourceService;
import com.jintoufs.web.action.shiro.bind.annotation.CurrentUser;
import org.cosmos.modules.web.msg.AjaxMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Controller
@Scope("prototype")
public class WelcomeAct {
    @Autowired
    private CustomerResourceService customerResourceService;

    @RequestMapping("isMenu")
    @ResponseBody
    public AjaxMsg isMenu(String pathname) {
        AjaxMsg ajaxMsg = new AjaxMsg();
        Map<String, Object> params = Maps.newHashMap();
        if (!(pathname.indexOf("/")==0)){
            pathname = "/"+pathname;
        }
        params.put("href", pathname);
        if (customerResourceService.isMenu(params)) {
            //在资源目录里
            ajaxMsg.setCode(AjaxMsg.SUCCESS);
        } else {
            ajaxMsg.setCode(AjaxMsg.FAILURE);
        }
        return ajaxMsg;
    }
    @RequestMapping("getMenu")
    public void getMenu(@CurrentUser Customer loginUser, HttpServletResponse response) {
        if (loginUser != null) {
            int roleId = loginUser.getRoleId();
            List<CustomerResource> list = customerResourceService.selectByRole(roleId);
            JhjResponseUtil.responseJson(response, JSON.toJSONString(list));
        }
    }

    @RequestMapping("welcome")
    public String welcome() {
        return "welcome";
    }

    @RequestMapping("error")
    public String error() {
        return "error/404";
    }

    @RequestMapping("exception")
    public String exception() {
        return "error/500";
    }

    @RequestMapping("unauthorize")
    public String unauthorize() {
        return "error/unauthorized";
    }
}
